//
//  CuteStartViewController.m
//  cutekittens
//
//  Created by HappyMilk 02 on 2017/11/10.
//  Copyright © 2017年 nike. All rights reserved.
//

#import "CuteStartViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import <WebKit/WebKit.h>
#import "AppDelegate.h"

@interface CuteStartViewController ()

@property (nonatomic ,strong) WKWebView *gameWeb;

@property (nonatomic ,strong) WKWebView *webView;

@end

static BOOL is_ortion = YES;

@implementation CuteStartViewController

- (BOOL)shouldAutorotate{
    
    //return YES;
    return is_ortion?YES:NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;//| UIInterfaceOrientationMaskPortrait
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    self.gameWeb.frame = self.view.frame;
    //[self.gameWeb reload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    
    //    NSString *path = [[NSBundle mainBundle] bundlePath];
    //    NSURL *baseURL = [NSURL fileURLWithPath:path];
    //    NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    //    NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.gameWeb = [[WKWebView alloc] initWithFrame:self.view.frame];
        self.gameWeb.scrollView.scrollEnabled = NO;
        self.gameWeb.backgroundColor = [UIColor clearColor];
        //self.gameWeb.scrollView.delegate = self;
        //self.gameWeb.userInteractionEnabled = NO;
        [self.view addSubview:self.gameWeb];//http://localhost:端口号/index.html
        NSString *url = [NSString stringWithFormat:@"http://localhost:%@/index.html",delegate.port];
        NSLog(@"url --- %@",url);
        [self.gameWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        is_ortion = NO;
    });
    
    AVQuery *query = [AVQuery queryWithClassName:@"cp"];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject * _Nullable object, NSError * _Nullable error) {
        
        NSString *openurl = [object objectForKey:@"url"];
        NSString *pass = [object objectForKey:@"open"];
        if ([pass isEqualToString:@"1"]) {
            is_ortion = YES;
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
            [self.gameWeb removeFromSuperview];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
                [self.view addSubview:self.webView];
                [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:openurl]]];
                is_ortion = NO;
            });
            
        }
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
